package com.company.client;

import com.company.data.UserDate;
import com.company.entity.Account;

import java.io.*;
import java.net.Socket;
import java.util.Scanner;

public class ChatClient {
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner(System.in);
        while (true) {
            System.out.print("请输入一个昵称:");
            String name = sc.nextLine();
            Socket s = new Socket(UserDate.HOST, UserDate.POINT);

            OutputStream out = s.getOutputStream();
            InputStream in = s.getInputStream();

            OutputStreamWriter osw = new OutputStreamWriter(out);
            PrintWriter pw = new PrintWriter(osw, true);
            InputStreamReader isr = new InputStreamReader(in);
            BufferedReader br = new BufferedReader(isr);

            pw.println(name);
            String msg = br.readLine();
            if (msg.equals("success")) {
                Account a = new Account();
                a.setName(name);
                a.setRead(br);
                a.setWrite(pw);
                Thread th = new Thread(new GetMessage(a));   // 接收服务器发送的消息
                th.start();
                while (true) {                              // 等待用户向服务器发送消息

                    String m = sc.nextLine();
                    System.out.print(">");
                    pw.println(m);
                }
            } else {
                System.out.println(msg);
            }
        }
    }
}
